/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.cli.console;

import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

/**
 *
 * @author stephane
 */
class PrintStreamConsole extends PrintStream {

    private JTextPane _area = null;

    public PrintStreamConsole(OutputStream os) {
        super(os);
    }

    public void Redirect(String n) {
        if (_area != null) {
            StyledDocument doc = _area.getStyledDocument();
            try {
                if (!n.endsWith("\n")) {
                    println();
                }
                doc.insertString(doc.getLength(), n, doc.getStyle("clacOutput"));
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void println(String x) {
        Redirect(x);
    }

    @Override
    public void write(byte buf[], int off, int len) {
        Redirect(new String(buf, off, len));
    }

    @Override
    public void write(int b) {
        Redirect(new String(new char[]{(char) b}));
    }

    /**
     * @return the _area
     */
    public JTextPane getArea() {
        return _area;
    }

    /**
     * @param area the _area to set
     */
    public void setArea(JTextPane area) {
        this._area = area;
    }
}
